/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import org.balisunrise.application.Application;
import org.balisunrise.common.IconManager;
import org.balisunrise.common.Mode;
import org.balisunrise.swing.components.panel.layouts.MessengerLayoutBuilder;

/**
 *
 * @author glauco
 */
public class MessengerPanel extends JPanel{

    private JLabel lbMode;

    private IconManager<ImageIcon> icons;

    private Map<String,JLabel> messages;

    private MessengerLayoutBuilder layoutBuilder;

    public MessengerPanel(String name, MessengerLayoutBuilder layoutBuilder) {

        this.layoutBuilder = layoutBuilder;
        this.icons = Application.get().getIconManager();

        this.setName(name);
        this.getAccessibleContext().setAccessibleName(name);

        this.setBackground(new Color(255, 255, 255));
        this.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240), 3));
        this.setMaximumSize(new java.awt.Dimension(32767, 500));
        Dimension size = new Dimension(0, 75);
        //this.setPreferredSize(size);
        this.setMinimumSize(size);

        lbMode = new JLabel();
        lbMode.setFont(new Font("Tahoma", Font.BOLD, 11));
        lbMode.setIcon(icons.getIcon(icons.INSERT));
        lbMode.setText("Inserindo");
        String lbModName = name + ":labelMode";
        lbMode.setName(lbModName);
        
        messages = new HashMap<String, JLabel>();
    }

    public Map<String, JLabel> getMessages() {
        return messages;
    }

    public void setMessages(Map<String, JLabel> messages) {
        this.messages = messages;
    }

    public void setMode(int mode) {
        String text;
        switch(mode){
            case Mode.NEW:
                text = "Inserindo";
                lbMode.setIcon(icons.getIcon(icons.INSERT));
                break;
            case Mode.UPDATE:
                text = "Editando";
                lbMode.setIcon(icons.getIcon(icons.UPDATE));
                break;
            case Mode.DELETE:
                text = "Excluir?";
                lbMode.setIcon(icons.getIcon(icons.DELETE));
                break;
            case Mode.DISPLAY:
                text = "Visualizando";
                lbMode.setIcon(icons.getIcon(icons.DELETE));
                break;
            default:
                text = new String();
                lbMode.setIcon(null);
        }
        lbMode.setText(text);
        buildLayout();
    }

    public void addMessage(String id, String message, int level) {

        Icon icon = null;

        if (level < 0)
            level = 0;

        if (level > 3)
            level = 3;

        switch (level) {
            case 0:
                icon = icons.getIcon(icons.CONFIRM);
                break;
            case 1:
                icon = icons.getIcon(icons.INFORMATION);
                break;
            case 2:
                icon = icons.getIcon(icons.WARNING);
                break;
            case 3:
                icon = icons.getIcon(icons.ERROR);
                break;
        }

        JLabel msg = new JLabel(message, icon, SwingConstants.LEFT);

        String msgName = getName() + ":messageLabel_" + id;
        msg.setName(msgName);
        msg.getAccessibleContext().setAccessibleName(msgName);
        msg.getAccessibleContext().setAccessibleName(String.valueOf(level));

        //System.out.println("Message[" + id + "]: " + message);

        messages.put(id, msg);
        buildLayout();
    }

    public void removeMessage(String id) {
        messages.remove(id);
        buildLayout();
    }

    public void removeAllMessages() {
        messages.clear();
        buildLayout();
    }

    public void buildLayout(){
        layoutBuilder.setLbMode(lbMode);
        layoutBuilder.setMessages(messages);
        layoutBuilder.buildLayout(this);
    }
}
